#include "ToggleNode.h"
#include <osg/Geometry>

ToggleNode::ToggleNode()
    : m_highlight(osg::Vec4(0.0f, 0.0f, 0.7f, 1.0f)),
      m_state(false)
{
}

void ToggleNode::toggleHightlight()
{
    if(!m_state)
    {
        changeColor(m_highlight);
    }
    else
    {
        changeColor(m_color);
    }

    m_state = !m_state;
}

void ToggleNode::resetColor()
{
    changeColor(m_color);
    m_state = false;
}

void ToggleNode::changeColor(const osg::Vec4& color)
{
    for(unsigned int i = 0; i < this->getNumDrawables(); ++i)
    {
        osg::Geometry* geom = this->getDrawable(i)->asGeometry();
        if(geom)
        {
            osg::Vec4Array* colors = static_cast<osg::Vec4Array*>(geom->getColorArray());
            if(colors)
            {
                colors->at(0).set(color.r(), color.g(), color.b(), color.a());
                geom->dirtyDisplayList();
            }
        }
    }
}